import { FC, memo } from 'react';
import { MessageInfo, OutputRenderStatus } from '.';
import { ChatItemComponent } from './item';
import { isArray } from '@tarojs/shared';
import { LooseAny } from '@material/typeing';

interface ChatDataListProps {
    dataList: MessageInfo[];
    onStatusChange?: (type: OutputRenderStatus) => void;
}
export const ChatDataList: FC<ChatDataListProps> = function (props) {
    const { dataList = [], onStatusChange } = props;
    const newDataList = isArray(dataList) ? dataList.filter(Boolean) : [];
    console.log('日志----历史', newDataList);
    return (
        <>
            {newDataList.map((item, index) => {
                const isLast = index === 0;
                const key = (item.id ? item.id : `${item.MsgId}_${index}`) || index;
                const isAddStatus = !item.isHistory && item.chatStatus === 'ai' && isLast;
                const stateObj: LooseAny = isAddStatus ? { onStatusChange } : {};
                return (
                    <ChatItemComponent
                        key={key}
                        {...item}
                        {...stateObj}
                    ></ChatItemComponent>
                );
            })}
        </>
    );
};

export const ChatDataListComponent = memo(ChatDataList);
